package com.hw.one.dam2.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hw.one.dam2.dao.IPollutionDao;
import com.hw.one.dam2.param.PollutionParam;
import com.hw.one.dam2.param.RegionSiteParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.DataCalculateTypeEnum;
import com.hw.one.dam2.common.DataTypeEnum;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.dao.ISmallAirDao;
import com.hw.one.dam2.entity.AQIEntity;
import com.hw.one.dam2.entity.ContrastRatioEntity;
import com.hw.one.dam2.entity.IAQIEntity;
import com.hw.one.dam2.param.SmallAirParam;
import com.hw.one.dam2.util.AQIProcess;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.dam2.util.WeatherUtil;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.util.DateTimeUtil;

@Service(value = "smallAirService")
public class SmallAirService implements ISmallAirService {

	// 小型站 dao
	@Autowired
	private ISmallAirDao smallAirDao;

	// 污染源数据
	@Autowired
	private IPollutionDao pollutionDao;
	// 点位信息
	@Autowired
	private IRegionSiteDao regionSiteDao;

	// 数字保留两位小数
	DecimalFormat decimalformat_2_scale = new DecimalFormat("######0.00");
	DecimalFormat decimalformat_3_scale = new DecimalFormat("######0.000");

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map<String, Object>> findLast12HourRaiseDustHourDataBySiteCode(String siteCode) {

		List<Map> smallHourList = smallAirDao.findLast12HourSmallSiteHourDataBySiteCode(siteCode);

		List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();
		Map<String, Object> aqiMap = null;

		for (Map hourAirMap : smallHourList) {
			aqiMap = new HashMap<String, Object>();
			aqiMap.put("pm10", hourAirMap.get("PM10"));
			aqiMap.put("pm25", hourAirMap.get("PM25"));
			aqiMap.put("temp", hourAirMap.get("TEMP"));
			aqiMap.put("hum", hourAirMap.get("HUM"));
			aqiMap.put("wd", hourAirMap.get("WD"));
			aqiMap.put("ws", hourAirMap.get("WS"));
			aqiMap.put("siteCode", hourAirMap.get("SITECODE"));
			aqiMap.put("monitorTime", hourAirMap.get("MONITORTIME"));
			aqiList.add(aqiMap);
		}

		return aqiList;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void setAirQualityHBMethod(List<Map> curWeekList, List<Map> lastWeekList) {
		// 计算周环比
		for (Map curWeekMap : curWeekList) {
			// 点位编码
			String curSiteCode = curWeekMap.get("SITECODE").toString();
			String curAQI = curWeekMap.get("AQI").toString();
			String curCV = curWeekMap.get("COMPOSITEVALUE").toString();
			String curPM10 = curWeekMap.get("PM10").toString();
			String curPM25 = curWeekMap.get("PM25").toString();
			String curSO2 = curWeekMap.get("SO2").toString();
			String curNO2 = curWeekMap.get("NO2").toString();
			String curCO = curWeekMap.get("CO").toString();
			String curO3 = curWeekMap.get("O3_8").toString();
			// 上周的数据
			for (Map lastWeekPO : lastWeekList) {
				String lastSiteCode = lastWeekPO.get("SITECODE").toString();
				if (lastSiteCode.equals(curSiteCode)) {
					String lastAQI = lastWeekPO.get("AQI").toString();
					String lastCV = lastWeekPO.get("COMPOSITEVALUE").toString();
					String lastPM10 = lastWeekPO.get("PM10").toString();
					String lastPM25 = lastWeekPO.get("PM25").toString();
					String lastSO2 = lastWeekPO.get("SO2").toString();
					String lastNO2 = lastWeekPO.get("NO2").toString();
					String lastCO = lastWeekPO.get("CO").toString();
					String lastO3 = lastWeekPO.get("O3_8").toString();
					// 计算aqi的环比值
					if (null != curAQI && null != lastAQI && !"0".equals(lastAQI)) {
						double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
								* 100;
						curWeekMap.put("aqiHB", decimalformat_2_scale.format(aqiRetio));
					}
					// 计算综合指数的环比值
					if (null != curCV && null != lastCV && !"0".equals(lastCV)) {
						double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
								* 100;
						curWeekMap.put("cvHB", decimalformat_2_scale.format(CVRetio));
					}
					// pm10的环比值
					if (null != curPM10 && null != lastPM10 && !"0".equals(lastPM10)) {
						double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
								/ Double.valueOf(lastPM10) * 100;
						curWeekMap.put("pm10HB", decimalformat_2_scale.format(PM10Retio));
					}
					// pm25的环比值
					if (null != curPM25 && null != lastPM25 && !"0".equals(lastPM25)) {
						double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
								/ Double.valueOf(lastPM25) * 100;
						curWeekMap.put("pm25HB", decimalformat_2_scale.format(PM25Retio));
					}
					// SO2的环比值
					if (null != curSO2 && null != lastSO2 && !"0".equals(lastSO2)) {
						double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
								* 100;
						curWeekMap.put("so2HB", decimalformat_2_scale.format(SO2Retio));
					}
					// NO2的环比值
					if (null != curNO2 && null != lastNO2 && !"0".equals(lastNO2)) {
						double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
								* 100;
						curWeekMap.put("no2HB", decimalformat_2_scale.format(NO2Retio));
					}
					// CO的环比值
					if (null != curCO && null != lastCO && !"0".equals(lastCO)) {
						double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
								* 100;
						curWeekMap.put("coHB", decimalformat_2_scale.format(CORetio));
					}
					// O3的环比值
					if (null != curO3 && null != lastO3 && !"0".equals(lastO3)) {
						double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Double.valueOf(lastO3)
								* 100;
						curWeekMap.put("o3HB", decimalformat_2_scale.format(O3Retio));
					}
					break;
				}
			}
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void setAirQualityTBMethod(List<Map> curWeekList, List<Map> lastYearList) {
		// 计算周同比
		for (Map curWeekMap : curWeekList) {
			// 点位编码
			String curSiteCode = curWeekMap.get("SITECODE").toString();
			String curAQI = curWeekMap.get("AQI").toString();
			String curCV = curWeekMap.get("COMPOSITEVALUE").toString();
			String curPM10 = curWeekMap.get("PM10").toString();
			String curPM25 = curWeekMap.get("PM25").toString();
			String curSO2 = curWeekMap.get("SO2").toString();
			String curNO2 = curWeekMap.get("NO2").toString();
			String curCO = curWeekMap.get("CO").toString();
			String curO3 = curWeekMap.get("O3_8").toString();
			// 上年的周数据
			for (Map lastYearMap : lastYearList) {
				String lastSiteCode = lastYearMap.get("SITECODE").toString();
				if (lastSiteCode.equals(curSiteCode)) {
					String lastAQI = lastYearMap.get("AQI").toString();
					String lastCV = lastYearMap.get("COMPOSITEVALUE").toString();
					String lastPM10 = lastYearMap.get("PM10").toString();
					String lastPM25 = lastYearMap.get("PM25").toString();
					String lastSO2 = lastYearMap.get("SO2").toString();
					String lastNO2 = lastYearMap.get("NO2").toString();
					String lastCO = lastYearMap.get("CO").toString();
					String lastO3 = lastYearMap.get("O3_8").toString();
					// 计算aqi的同比
					if (null != curAQI && null != lastAQI && !"0".equals(lastAQI)) {
						double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
								* 100;
						curWeekMap.put("aqiTB", decimalformat_2_scale.format(aqiRetio));
					}
					// 计算综合指数的同比
					if (null != curCV && null != lastCV && !"0".equals(lastCV)) {
						double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
								* 100;
						curWeekMap.put("cvTB", decimalformat_2_scale.format(CVRetio));
					}
					// pm10的同比
					if (null != curPM10 && null != lastPM10 && !"0".equals(lastPM10)) {
						double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
								/ Double.valueOf(lastPM10) * 100;
						curWeekMap.put("pm10TB", decimalformat_2_scale.format(PM10Retio));
					}
					// pm25的同比
					if (null != curPM25 && null != lastPM25 && !"0".equals(lastPM25)) {
						double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
								/ Double.valueOf(lastPM25) * 100;
						curWeekMap.put("pm25TB", decimalformat_2_scale.format(PM25Retio));
					}
					// SO2的同比
					if (null != curSO2 && null != lastSO2 && !"0".equals(lastSO2)) {
						double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
								* 100;
						curWeekMap.put("so2TB", decimalformat_2_scale.format(SO2Retio));
					}
					// NO2的同比
					if (null != curNO2 && null != lastNO2 && !"0".equals(lastNO2)) {
						double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
								* 100;
						curWeekMap.put("no2TB", decimalformat_2_scale.format(NO2Retio));
					}
					// CO的同比
					if (null != curCO && null != lastCO && !"0".equals(lastCO)) {
						double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
								* 100;
						curWeekMap.put("coTB", decimalformat_2_scale.format(CORetio));
					}
					// O3的同比
					if (null != curO3 && null != lastO3 && !"0".equals(lastO3)) {
						double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Integer.valueOf(lastO3)
								* 100;
						curWeekMap.put("o3TB", decimalformat_2_scale.format(O3Retio));
					}
					break;
				}
			}
		}
	}

	public Map<String, Object> findHotPointDataWithSmallAir(Map<String, Object> paramMap) throws Exception {
		String regionCode = (String) paramMap.get("regionCode");
		String startTime = (String) paramMap.get("startTime");
		String endTime = (String) paramMap.get("endTime");
//		Map<String, String> siteParamMap = new HashMap<>();
//		siteParamMap.put("regionCode", regionCode);
//		siteParamMap.put("siteType", SiteTypeEnum.SMALL_AIR_SITE.getValue());
		RegionSiteParam siteParam = new RegionSiteParam();
		siteParam.setRegionCode(regionCode);
		siteParam.setSiteType(SiteTypeEnum.SMALL_AIR_SITE.getValue());
		// 查询当前区域下的小型站信息
		List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);
		List<String> siteCodeList = new ArrayList<>();
		for (Map<String, Object> siteInfo : siteInfoList) {
			siteCodeList.add((String) siteInfo.get("siteCode"));
		}
		SmallAirParam smallAirParam = new SmallAirParam();
		smallAirParam.setSiteCodes(siteCodeList);
		smallAirParam.setStartTime(startTime);
		smallAirParam.setEndTime(endTime);
		// 根据时间和小型站编号查询5分钟数据
		List<Map> hourDataList = smallAirDao.findSmallMinuteDataByPointCodeAndTime(smallAirParam);
		// 组装前端需要的数据
		List<Map<String, Object>> hotPointList = new ArrayList<>();

		List<String> hourDataCodeList = new ArrayList<>();

		for (Map hourData : hourDataList) {
			String hourDataCode = hourData.get("SITECODE").toString();
			if (hourDataCodeList.contains(hourDataCode)) {
				continue;
			}
			hourDataCodeList.add(hourDataCode);
			Map<String, Object> hotPoint = new HashMap<>();
			for (Map<String, Object> siteInfo : siteInfoList) {
				if (siteInfo.get("siteCode").toString().equals(hourDataCode)) {
					hotPoint.put("count", hourData.get("AQI"));
					hotPoint.put("lat", siteInfo.get("Latitude"));
					hotPoint.put("lng", siteInfo.get("Longitude"));
				}

			}
			hotPointList.add(hotPoint);
		}
		// 组装数据
		Map<String, Object> windResult = new HashMap<>();
		windResult.put("ws", "0.2806");
		windResult.put("wd", "东北");

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("hotPointList", hotPointList);
		resultMap.put("windResult", windResult);

		return resultMap;
	}

	@Override
	public List<Map<String, Object>> findNewMinuteRaiseDustDataBySiteCode() {
		Map<String, Object> region = UserUtil.getRegion();
		String userRegionCode = (String) region.get("regionCode");
		String userRegionType = (String) region.get("regionType");
//		Map<String, String> searchSiteInfoMap = new HashMap<>();
//		searchSiteInfoMap.put("monitorCode", "1000"); // 获取视频数据
//		searchSiteInfoMap.put("regionCode", userRegionCode);
//		searchSiteInfoMap.put("siteType", "raiseDustSite"); // 扬尘

		RegionSiteParam siteParam = new RegionSiteParam();
		siteParam.setRegionCode(userRegionCode);
		siteParam.setSiteType("raiseDustSite");
		siteParam.setMonitorCode("10000");
		List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);
		if (siteInfoList == null || siteInfoList.size() == 0) {
			return null;
		}

		// 点位编码列
		List<String> pointCodeList = new ArrayList<String>();
		for (Map<String, Object> siteMap : siteInfoList) {
			pointCodeList.add(String.valueOf(siteMap.get("siteCode")));
		}

		List<Map> raiseDustList = smallAirDao.findSmallAirNewMinuteDataBySiteCodeList(pointCodeList);

		List<String> haveCodeList = new ArrayList<>();
		for (Map<String, Object> siteInfo : siteInfoList) {
			String siteCode = (String) siteInfo.get("siteCode");
			for (Map raiseDust : raiseDustList) {
				if (siteCode.equals(raiseDust.get("SITECODE"))) {
					if (haveCodeList.contains(siteCode)) {
						continue;
					}
					haveCodeList.add(siteCode);
					siteInfo.put("PM10", raiseDust.get("PM10"));
					siteInfo.put("PM25", raiseDust.get("PM25"));
					siteInfo.put("NOISE", raiseDust.get("NOISE"));
				}
			}
		}

		return siteInfoList;
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void setSiteNameMetiod(List<Map<String, Object>> siteInfoList, List<Map> curWeekList) {
		// 排名
		int index = 1;
		/**
		 * 设置点位名称、同比值、环比值
		 */
		for (Map weekMap : curWeekList) {
			String weekSiteCode = weekMap.get("SITECODE").toString();
			weekMap.put("index", index++);
			// 设置点位名称
			for (Map<String, Object> siteMap : siteInfoList) {
				String siteCode = String.valueOf(siteMap.get("siteCode"));
				if (weekSiteCode.equals(siteCode)) {
					weekMap.put("siteName", siteMap.get("siteName"));
					weekMap.put("regionName", siteMap.get("RegionName"));
					break;
				}
			}
		}
	}

}
